import { getAllRoutes } from '@/utils/access'
var menu = {
  namespaced: true,
  state: {
    // 经过权限判断后的路由列表
    accessMenu: []
  },
  mutations: {
    setAccessMenu: (state, access) => {
      var allMenu = getAllRoutes()

      var getMenu = (list) => {
        var arr = []
        list.forEach(item => {
          var tempData = {}
          if (item.meta && item.meta.access && item.meta.access.length > 0) {
            var accessName = item.meta.access[0]
            if (access.indexOf(accessName) !== -1) {
              tempData = Object.assign({}, item)
            }
          } else {
            tempData = Object.assign({}, item)
          }
          // 当前的项目经过判断后，如果path和name不存在，则当前未通过权限判断
          // 有些路由只有path，并没有name，这种情况应该认为通过判断
          if (tempData.path || tempData.name) {
            if (item.children && item.children.length > 0) {
              tempData.children = getMenu(item.children)
            }
            arr.push(tempData)
          }
        })
        return arr
      }
      var accessMenuList = getMenu(allMenu)
      state.accessMenu = accessMenuList
    }
  },
  actions: {
    getAccessMenu({ commit }, access) {
      return new Promise((resolve, reject) => {
        commit('setAccessMenu', access)
        resolve()
      })
    }
  }
}

export default menu
